package com.example.administrator.biyesheji_print.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.biyesheji_print.R;
import com.example.administrator.biyesheji_print.Utils.SharedPreferencesUtils;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2018/3/31.
 */

public class mPrintAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ArrayList<HashMap<String,Object>>  strings;
    private Context context;
    private Fragment mframent;
    OnItemClickListener mOnItemClickListener;
    public mPrintAdapter(ArrayList<HashMap<String,Object>> strings, Context context, Fragment mframent) {
        this.context=context;
        this.mframent=mframent;
        this.strings=strings;
    }
    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }
    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener){
        this.mOnItemClickListener=mOnItemClickListener;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.print_item, parent, false);
        ViewHolder viewHolder=new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        SharedPreferencesUtils.putInt(context,"position",position);
        ((ViewHolder)holder).tv_file.setText(strings.get(position).get("filename").toString());
        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(holder.itemView,pos);
                }
            });
        }

        if (strings.get(position).get("filename").toString()=="请选择文件"){
            ((ViewHolder)holder).iv_fileselect.setVisibility(View.VISIBLE);
            ((ViewHolder)holder).iv_fileddelete.setVisibility(View.GONE);
        }else{
            ((ViewHolder)holder).iv_fileselect.setVisibility(View.GONE);
            ((ViewHolder)holder).iv_fileddelete.setVisibility(View.VISIBLE);
        }
        ((ViewHolder)holder).iv_fileselect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                // intent.putExtra("position", position);
                intent.setType("text/plain");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                SharedPreferencesUtils.putInt(context,"position",position);
                mframent.startActivityForResult(intent,1);
            }
        });

    }

    @Override
    public int getItemCount() {
        return strings==null?0:strings.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_file;
        ImageView iv_fileselect;
        ImageView iv_fileddelete;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_file = itemView.findViewById(R.id.tv_file);
           iv_fileselect= itemView.findViewById(R.id.iv_fileselect);
            iv_fileddelete=itemView.findViewById(R.id.iv_fileddelete);
        }
    }
}
